package com.rongshu.view.userInfo.dialog;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.support.v4.content.ContextCompat;
import android.text.method.LinkMovementMethod;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.rongshu.R;
import com.rongshu.utils.Common;
import com.rongshu.utils.Constant;
import com.rongshu.utils.SpannableStringUtils;
import com.rongshu.view.userInfo.listener.OnPayClickListener;
import com.rongshu.view.web.LocalWebActivity;

import butterknife.BindView;
import butterknife.ButterKnife;

public class PayDialog extends Dialog {
    @BindView(R.id.txt_pay_explain)
    TextView txt_pay_explain;
    @BindView(R.id.iv_close)
    LinearLayout iv_close;
    @BindView(R.id.txt_money)
    TextView txt_money;
    @BindView(R.id.txt_pay_detail)
    TextView txt_pay_detail;
    @BindView(R.id.txt_pay)
    TextView txt_pay;
    //view
    private View view = null;


    public PayDialog(Context context) {
        super(context, R.style.full_width_dialog_at_screen_bottom);
        view = View.inflate(context, R.layout.fragment_pay_dialog_view, null);
        setContentView(view);
        ButterKnife.bind(this, view);
        WindowManager.LayoutParams layoutParams = this.getWindow().getAttributes();
        layoutParams.gravity = Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL;
        layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
        layoutParams.height = ViewGroup.LayoutParams.WRAP_CONTENT;
        setCanceledOnTouchOutside(true);
    }

    public static class Builder {
        private PayDialog dialog;
        private Context context;
        //money
        private double money = 0.01;
        //banyan_tree_leaves
        private int banyan_tree_leaves = 0;
        //listener
        private OnPayClickListener mOnPayClickListener = null;

        public Builder(Context context) {
            this.context = context;
        }

        public Builder setMoney(double money) {
            this.money = money;
            return this;
        }

        public Builder setBanyan_tree_leaves(int banyan_tree_leaves) {
            this.banyan_tree_leaves = banyan_tree_leaves;
            return this;
        }

        /**
         * set listener
         */
        public Builder setOnPayClickListener(OnPayClickListener listener) {
            this.mOnPayClickListener = listener;
            return this;
        }

        public PayDialog create() {
            if (dialog == null) {
                dialog = new PayDialog(context);
            }
            //充值说明
            String content = dialog.txt_pay_explain.getText().toString().trim();
            dialog.txt_pay_explain.setText(SpannableStringUtils.addForeColorSpan(content,
                    ContextCompat.getColor(context, R.color.color_3cc051),
                    11, new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Intent intent = new Intent(context, LocalWebActivity.class);
                            intent.putExtra(Constant.INTENT_TITLE, "榕树叶充值使用说明");
                            intent.putExtra(Constant.INTENT_H5, Common.PAY_EXPLAIN);
                            context.startActivity(intent);
                        }
                    }));
            dialog.txt_pay_explain.setMovementMethod(LinkMovementMethod.getInstance());
            dialog.txt_pay_explain.setHighlightColor(ContextCompat.getColor(context, android.R.color.transparent));
            //支付
            dialog.txt_pay.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mOnPayClickListener != null) {
                        mOnPayClickListener.onPayClick(dialog);
                    }
                }
            });
            //关闭
            dialog.iv_close.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dialog.dismiss();
                }
            });
            //money
            dialog.txt_money.setText(money + "");
            //banyan_tree_leaves
            dialog.txt_pay_detail.setText("充值" + (int) money + "元即可获得" + banyan_tree_leaves + "榕树叶");
            return dialog;
        }
    }
}
